#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from pyrogram import Client, filters
from pyrogram.errors import FloodWait
from pyrogram.errors import SlowmodeWait

from datetime import datetime
from time import sleep
from progress.bar import IncrementalBar

from os import remove
import json

app = Client("my_account", sleep_threshold=0)

chat_id_list = []

def menu():
    chats = input(
"""
Введіть публічне посилання на чат або його id
(Наприклад: -1223447675434 або membersofxaknet 
   або -1223447675434, -17654345438, hz_group)
: """
    ).replace(" ", "").split(",")
    for chat in chats:
        try:
            chat_id_list.append(int(chat))
        except:
            chat_id_list.append(chat)
menu()

def getmember(member):
    data = {
        "tg_id": "",
        "first_name": "",
        "last_name": "",
        "username": "",
        "phone_number": "",
        "status": "",
        "title": "",
        "joined_date": ""
    }
    if member.user.id:
        data["tg_id"] = member.user.id
    if member.user.first_name:
        data["first_name"] = member.user.first_name
    if member.user.last_name:
        data["last_name"] = member.user.last_name
    if member.user.username:
        data["username"] = member.user.username
    if member.user.phone_number:
        data["phone_number"] = member.user.phone_number
    if member.status:
        status = member.status
        if member.status == "administrator":
            status = "admin"
        data["status"] = status
    if member.title:
        data["title"] = member.title
    if member.joined_date:
        utc_time = datetime.utcfromtimestamp(member.joined_date)
        utc_time = utc_time.strftime("%Y-%m-%d %H:%M:%S.%f+00:00 (UTC)")
        data["joined_date"] = utc_time
    return data
def allwork(chat_id_list):
    for chat_id in chat_id_list:
        chat_name = app.get_chat(chat_id).title
        print(f'Отримання даних учасників чату {chat_name}')
        count = app.get_chat_members_count(chat_id)
        class MyBar(IncrementalBar):
            if count < 70:
                width = count
            else:
                width = 70
            suffix = '%(index)d/%(max)d'
            bar_prefix = '▐'
            bar_suffix = '▌'
            empty_fill = '-'
            phases = ("░","▒","█")
        bar = MyBar("", max = count)
        i = 1 
        members_list = []
        admin_list = []
        creator = {}
        for member in app.iter_chat_members(chat_id):
            data = getmember(member)
            members_list.append(data)
            if data["status"] == 'admin':
                admin_list.append(data)
            if data["status"] == 'creator':
                creator = data
            bar.next()
            i += 1
            if i % 3000 == 0:
                sleep(10)
                print("\nFloodWait 10 sec")
        bar.finish()
        if not admin_list:
            print("Адмінів не знайдно, або у них увімкнена анонімність")
        else:
            print(f"Записую адмінів у файл '{chat_id}-admin.json'")
            with open(f"{chat_id}_admin.json", 'w', encoding="utf8") as outfile:
                json.dump(admin_list, outfile, indent=4, ensure_ascii=False)
        if not creator:
            print("Власника не знайдно, або у нього увімкнена анонімність")
        else:
            print(f"Записую власника у файл '{chat_id}-creator.json'")
            with open(f"{chat_id}-creator.json", 'w', encoding="utf8") as outfile:
                json.dump(creator, outfile, indent=4, ensure_ascii=False)
            
        print(f"Записую всіх у файл '{chat_id}-all.json'")
        with open(f"{chat_id}-all.json", 'w', encoding="utf8") as outfile:
            json.dump(members_list, outfile, indent=4, ensure_ascii=False)
        
        print("Завершено")
        app.stop()
        exit(0)

with app:
    allwork(chat_id_list)

app.run()
